
--[[
	***********************
	* MCServer - thomasfn *
	***********************
	hooks.lua - Loads the hooks module
--]]

hooks = {}

local HookTable = {}
local DefaultHooks = {}

function hooks.Add( id, tbl )
	if (id == "default") then
		table.Merge( DefaultHooks, tbl )
	else
		HookTable[ id ] = tbl
	end
end

function hooks.Remove( id )
	HookTable[ id ] = nil
end

function hooks.Call( name, argtbl )
	for id, tbl in pairs( HookTable ) do
		local func = HookTable[ id ][ name ]
		if (func) then
			local b, res = pcall( func, unpack( argtbl ) )
			if (not b) then
				print( "Warning: Hook '" .. name .. "' reported an error, hook table '" .. id .. "' was removed." )
				print( res )
				hooks.Remove( id )
			end
			if (res ~= nil) then return res end
		end
	end
	local func = DefaultHooks[ name ]
	if (not func) then return end
	local b, res = pcall( func, unpack( argtbl ) )
	if (not b) then
		print( "Warning: Hook '" .. name .. "' reported an error, default hook table." )
		print( res )
		return
	end
	return res
end